<template>
  <div class="login-container">
    <div class="wrapper-page">
      <div class="card">
        <div class="card-body">
          <h4 class="title">
            <b>{{ $t('login.title') }}</b>
          </h4>
          <div>
            <el-form
              ref="loginForm"
              :model="loginForm"
              :rules="loginRules"
              class="login-form"
              autocomplete="on"
              label-position="left"
              @keyup.enter.native="handleLogin"
            >
              <el-form-item prop="username">
                <span class="svg-container">
                  <svg-icon icon-class="user" />
                </span>
                <el-input
                  ref="username"
                  v-model.trim="loginForm.username"
                  :placeholder="$t('login.username')"
                  type="text"
                  tabindex="1"
                />
              </el-form-item>
              <el-tooltip
                v-model="showCapsTooltip"
                content="Caps lock is On"
                placement="right"
                manual
              >
                <el-form-item prop="password">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input
                    ref="password"
                    v-model.trim="loginForm.password"
                    :type="passwordTypeFlag? 'password' : 'text'"
                    :placeholder="$t('login.password')"
                    tabindex="2"
                    autocomplete="new-password"
                    @keyup.native="checkCapslock"
                    @blur="showCapsTooltip = false"
                  />
                </el-form-item>
              </el-tooltip>
              <el-button
                :loading="loading"
                type="primary"
                style="width:100%;margin-bottom:30px;"
                @click="handleLogin"
              >{{ $t('login.logIn') }}</el-button>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>

import { setRouterPermissions } from '@/utils/auth'

export default {
  name: 'login',
  components: {},
  data() {
    return {
      loading: false,
      loginForm: {
        username: 'admin',
        password: '12345678'
      },
      passwordTypeFlag: true,
      showCapsTooltip: false

    }
  },
  computed: {
    loginRules() {
      return {
        username: [{ required: true, trigger: 'blur', message: this.$t('login.usernameTip') }],
        password: [{ required: true, trigger: 'blur', message: this.$t('login.passwordTip') }]
      }
    }
  },
  mounted() {
    this.loginForm.username === '' ? this.$refs.username.focus() : this.$refs.password.focus()
  },
  methods: {

    checkCapslock({ shiftKey, key } = {}) {
      // shiftKey 是否是键盘事件
      // key 是按下的键盘值 tab、A 等
      if (key && key.length === 1 && (shiftKey && (key >= 'a' && key <= 'z') || !shiftKey && (key >= 'A' && key <= 'Z'))) {
        this.showCapsTooltip = true
      } else {
        this.showCapsTooltip = false
      }
    },
    togglePwdType() {
      this.passwordTypeFlag = !this.passwordTypeFlag
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm)
            .then(response => {
              // 登录成功，存储用户信息，设置菜单权限，按钮权限
              setRouterPermissions(JSON.stringify(response.data.menuPermission || []))
              this.$store.dispatch('permission/setButtonPermission', response.data.buttonPermission).then(() => {
                // 跳转到被重定向进来的页面
                const { path, query } = this.getRedirectRoute(this.$route)
                this.$router.push({ path, query })
              })
            })
            .finally(() => {
              this.loading = false
            })
        }
      })
    },
    getRedirectRoute(route) {
      let path = '/'
      let query = {}
      if (route.query) {
        path = route.query.redirect ? route.query.redirect : '/'
        query = Object.keys(route.query).reduce((acc, cur) => {
          if (cur !== 'redirect') {
            acc[cur] = route.query[cur]
          }
          return acc
        }, {})
      }
      return { path, query }
    }
  }
}
</script>

<style lang="scss" >
/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 40px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      border-radius: 0px;
      height: 38px;
      padding-left: 10px;
    }
  }
  .el-form-item {
    border: 1px solid rgba(0, 0, 0, 0.2);
    border-radius: 5px;
    height: 40px;
    box-sizing: border-box;
    .el-form-item__content {
      height: 38px;
      line-height: 38px;
    }
  }
}
</style>

<style lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #eee;
.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;
  .wrapper-page {
    margin: 13% auto;
    max-width: 400px;
    .card {
      box-shadow: 1px 0px 20px rgba(0, 0, 0, 0.9);
      background-color: #fff;
      background-clip: border-box;
      border-radius: 0.25rem;
      .card-body {
        padding: 1.25rem;
        .logo {
          margin-bottom: 15px;
          text-align: center;
          line-height: 30px;
        }
        .title {
          text-align: center;
          line-height: 22px;
          font-size: 18px;
          color: #707070;
          .set-language {
            float: right;
            font-size: 18px;
            cursor: pointer;
          }
        }
        .svg-container {
          padding-left: 15px;
          color: $dark_gray;
          vertical-align: middle;
          width: 30px;
          display: inline-block;
        }
      }
    }
  }
}
</style>
